package com.weharvest.edu.controller;


import com.weharvest.edu.annotation.MenuAnnotion;
import com.weharvest.edu.annotation.PermissionAnnotion;
import com.weharvest.edu.annotation.SignSecurity;
import com.weharvest.edu.constant.MenuConstant;
import com.weharvest.edu.constant.PermissionConstants;
import com.weharvest.edu.dto.BackstageRoleAndMenuAndPermissionDTO;
import com.weharvest.edu.dto.BackstageRoleDTO;
import com.weharvest.edu.enums.ResultEnum;
import com.weharvest.edu.service.BackstageRoleService;
import com.weharvest.edu.utlis.ResultUtil;
import com.weharvest.edu.view.BackstageUserView;
import com.weharvest.edu.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/backstage/role")
@SignSecurity
@MenuAnnotion(value = MenuConstant.BACKSTAGE_ROLE)
@Slf4j
public class BackstageRoleController {

    @Resource
    private BackstageRoleService backstageRoleService;


    //查看角色列表
    @PostMapping("/find")
    @ResponseBody
    @PermissionAnnotion(value = PermissionConstants.DATA_FIND)
    public ResultVo<BackstageUserView> find(
            @RequestBody BackstageRoleDTO dto,
            HttpServletRequest request, HttpServletResponse response) {
        return ResultUtil.success(backstageRoleService.findByAll(dto, request, response));
    }

    //授权角色-查看所有该角色所拥有的资源
    @PostMapping("/authorization/find")
    @ResponseBody
    @PermissionAnnotion(value = PermissionConstants.DATA_FIND)
    public ResultVo authorizationByFind(@RequestBody BackstageRoleDTO dto) {
        return ResultUtil.success(backstageRoleService.authorizationByFind(dto));
    }

    //授权角色-点击某个角色，查看所有该角色的资源
    @PostMapping("/authorization/find/one")
    @ResponseBody
    @PermissionAnnotion(value = PermissionConstants.DATA_FIND)
    public ResultVo authorizationByFindOne(@RequestBody BackstageRoleDTO dto) {
        return ResultUtil.success(backstageRoleService.authorizationByFindOne(dto));
    }

    //授权角色
    @PostMapping("/authorization/add")
    @ResponseBody
    @PermissionAnnotion(value = PermissionConstants.DATA_PERMISSION)
    public ResultVo authorizationByAdd(
            @RequestBody BackstageRoleAndMenuAndPermissionDTO bRMP) {
        backstageRoleService.authorizationByAdd(bRMP);
        return ResultUtil.success(ResultEnum.SUCCESS.SUCCESS_AUTH);
    }

    //删除角色
    @PostMapping("/delete")
    @ResponseBody
    @PermissionAnnotion(value = PermissionConstants.DATA_REMOVE)
    public ResultVo delete(
            @RequestBody BackstageRoleDTO dto,
            HttpServletRequest request, HttpServletResponse response
    ) {
        backstageRoleService.delete(dto, request, response);
        return ResultUtil.success(ResultEnum.SUCCESS_DELETE);
    }


    /**
     * 修改角色
     *
     * @param backstageRoleDTO
     * @return
     */
    //  @Valid BackstageRoleForm backstageRoleForm, BindingResult bindingResult
    @PostMapping("/modify")
    @ResponseBody
    @PermissionAnnotion(value = PermissionConstants.DATA_MODIFY)
    public ResultVo modify(@RequestBody BackstageRoleDTO backstageRoleDTO) {
        backstageRoleService.modify(backstageRoleDTO);
        return ResultUtil.success(ResultEnum.SUCCESS_MODIFY);
    }

    /**
     * 新增角色
     *
     * @param backstageRoleDTO
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    @PermissionAnnotion(value = PermissionConstants.DATA_ADD)
    public ResultVo add(@RequestBody BackstageRoleDTO backstageRoleDTO, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        return ResultUtil.success(ResultEnum.SUCCESS_ADD);
    }

}
